package com.HuangJiaQing.www.service.impl;

import com.HuangJiaQing.www.dao.GoodsMapper;
import com.HuangJiaQing.www.dao.MarketersMapper;
import com.HuangJiaQing.www.model.Goods;
import com.HuangJiaQing.www.model.GoodsExample;
import com.HuangJiaQing.www.model.Marketers;
import com.HuangJiaQing.www.model.MarketersExample;
import com.HuangJiaQing.www.service.MarketersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/7/29 15:17
 */
@Service
public class MarketersServiceImpl implements MarketersService {
    @Autowired
    private MarketersMapper marketersMapper;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private GoodsMapper goodsMapper;

    public boolean registerMarketers(Marketers marketers) {
        MarketersExample marketersExample = new MarketersExample();
        marketersExample.createCriteria().andMarketersNameEqualTo(marketers.getMarketersName());
        List<Marketers> list = marketersMapper.selectByExample(marketersExample);
        if (list.isEmpty()) {
            marketersMapper.insertSelective(marketers);
            return true;
        } else {
            return false;
        }
    }

    public void updateMarketersForbidById(Integer id) {
        Marketers marketers = new Marketers();
        marketers.setMarketersId(id);
        marketers.setIsForbid(1);
        marketersMapper.updateByPrimaryKeySelective(marketers);
    }

    public void updateMarketersUnsealById(Integer id) {
        Marketers marketers = new Marketers();
        marketers.setMarketersId(id);
        marketers.setIsForbid(0);
        marketersMapper.updateByPrimaryKeySelective(marketers);
    }

    public List<Goods> marketersLogin() {
        Marketers marketers = (Marketers) httpSession.getAttribute("marketers");
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andMarketersNameEqualTo(marketers.getMarketersName());
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        return goods;
    }
}
